package com.china08.yxyapi.api.manage.tec.office.attendance;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.attendance.YxyYxyTecAttendanceConfig;
import com.china08.yxyapi.entity.mg.attendance.YxyTeaAttendance;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.attendance.YxyTecAttendanceConfigRepository;
import com.china08.yxyapi.repository.mg.attendance.YxyTeaAttendanceRepository;
import com.china08.yxyapi.security.profile.YxyProfile;
import com.china08.yxyapi.util.ClientIPUtil;
import com.china08.yxyapi.util.DateUtils;

/**
 * 教师考勤--我的考勤--打卡
 * 
 * @author LiuWeidong
 *
 */
@CrossOrigin
@RestController("tecAttendanceYxyTeaAttendanceCrtController")
public class AttendanceYxyTeaAttendanceCrtController extends YxyApiBaseController {

    @Autowired
    private YxyTeaAttendanceRepository yxyTeaAttendanceRepository;

    @Autowired
    private YxyTecAttendanceConfigRepository yxyTecAttendanceConfigRepository;

    @RequestMapping(value = "/api_yw/manage/tec/office/attendance/TeaAttendanceCrt", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(HttpServletRequest request, //
            HttpServletResponse response, //
            @RequestBody ReqModel reqModel, RespModel respModel) {

        YxyProfile yxyProfile = getProfile(request, response);
        String teacherId = yxyProfile.getId();
        String schoolId = yxyProfile.getSchoolId();

        // 获取系统时间
        Integer dayOfDate = DateUtils.formatDate4(new Date());

        YxyYxyTecAttendanceConfig yxyYxyTecAttendanceConfig = yxyTecAttendanceConfigRepository.findBySchoolId(schoolId);
        Assert.isTrue(null != yxyYxyTecAttendanceConfig, "管理员未设置打卡时间，不能进行打卡！");

        // 判断本系统网关和管理员设置网关是否一致
        if (yxyYxyTecAttendanceConfig.getGateway() == null) {
            Assert.isTrue(false, "请先通知管理员设置网关后再来打卡！");
        }
        if (!ClientIPUtil.getRealClientIP(request).equals(yxyYxyTecAttendanceConfig.getGateway())) {
            Assert.isTrue(false, "您当前使用的网络不在规定范围内,不能进行打卡!");
        }

        YxyTeaAttendance yxyTeaAttendance = new YxyTeaAttendance();
        yxyTeaAttendance.setTeacherId(yxyProfile.getId());
        yxyTeaAttendance.setSchoolId(yxyProfile.getSchoolId());
        yxyTeaAttendance.setSchoolName(yxyProfile.getSchoolName());
        yxyTeaAttendance.setTeacherName(yxyProfile.getUserNick());
        yxyTeaAttendance.setDayOfDate(DateUtils.formatDate4(new Date()));
        yxyTeaAttendance.setSignTime(new Date());
        yxyTeaAttendanceRepository.save(yxyTeaAttendance);

        Integer count = yxyTeaAttendanceRepository.countByDayOfDateAndTeacherIdAndSchoolId(dayOfDate, teacherId,
                schoolId);
        if (count == null) {
            count = 0;
        }
        RespModel model = new RespModel();
        model.count = count;
        return model;

    }

    protected static class ReqModel extends YxyApiReqModel {
    }

    protected static class RespModel extends YxyApiRespModel {
        // 打卡次数
        public Integer count;
        // 教师ID
        public String teacherId;
    }
}
